package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/12 21:23
 **/
public class Day10_2_Num297_Codec {
    public class Codec {

        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            String str = "";
            if(root == null) return "&,";
            str += root.val + ",";
            str += serialize(root.left);
            str += serialize(root.right);
            return str;
        }

        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            String[] ch = data.split(",");
            return deserializeCh(ch);
        }

        int index = -1;
        private TreeNode deserializeCh(String[] ch){
            index++;
            if(!ch[index].equals("&")){
                TreeNode node = new TreeNode(Integer.parseInt(ch[index]));
                node.left = deserializeCh(ch);
                node.right = deserializeCh(ch);
                return node;
            }else return null;
        }
    }
}